<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="题目名称" prop="questionTitle">
        <el-input
          v-model="queryParams.questionTitle"
          placeholder="请输入题目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="题目类型" prop="questionType">
        <el-select
          clearable
          v-model="queryParams.questionType"
          placeholder="请选择题目类型"
        >
          <el-option
            v-for="dict in dict.type.exam_question_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <!-- TODO 学科没权限得话这里就获取不了 -->
      <el-form-item label="学科" prop="subjectId">
        <el-select
          clearable
          v-model="queryParams.subjectId"
          placeholder="请选择学科"
        >
          <el-option
            v-for="item in subjectList"
            :key="item.subjectId"
            :label="item.subjectName"
            :value="item.subjectId"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="题目难度" prop="difficult">
        <el-select
          clearable
          v-model="queryParams.difficult"
          placeholder="请选择题目难度"
        >
          <el-option
            v-for="dict in dict.type.exam_question_difficult"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="题目总分" prop="questionScore">
        <el-input-number
          v-model="queryParams.questionScore"
          placeholder="请输入题目总分"
          :max="100"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb10">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['exam:question:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-tooltip
          class="item"
          effect="dark"
          content="导出数据由当前分页数据决定"
          placement="right"
        >
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['exam:question:export']"
            >导出</el-button
          >
        </el-tooltip>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getQuestionList"
      ></right-toolbar>
    </el-row>

    <el-table
      :border="true"
      v-loading="loading"
      :data="questionList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        v-if="checkPermi(['exam:question:remove', 'exam:question:export'])"
        type="selection"
        align="center"
      />

      <!--       <el-table-column label="主键" align="center" prop="questionId" /> -->
      <el-table-column sortable label="排序" align="center" prop="orderNum" />
      <el-table-column label="标题" align="center" prop="questionTitle">
        <template slot-scope="scope">
          <el-tooltip
            class="item"
            effect="dark"
            :content="scope.row.questionTitle"
            placement="right"
          >
            <span> {{ scope.row.questionTitle | ellipsis(0, 5) }}</span>
          </el-tooltip>
        </template>
      </el-table-column>

      <el-table-column label="类型" align="center" prop="questionType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.exam_question_type"
            :value="scope.row.questionType"
          />
        </template>
      </el-table-column>
      <el-table-column
        label="总分"
        sortable
        align="center"
        prop="questionScore"
      />
      <el-table-column label="难度" sortable align="center" prop="difficult">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.exam_question_difficult"
            :value="scope.row.difficult"
          />
        </template>
      </el-table-column>

      <el-table-column
        sortable
        label="创建时间"
        align="center"
        prop="createTime"
      ></el-table-column>
      <!--       
      <el-table-column label="更新时间" align="center" prop="updateTime" /> -->

      <el-table-column
        v-if="checkPermi(['exam:question:edit'])"
        label="状态"
        align="center"
        prop="status"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            @change="handleStatus(scope.row)"
            active-value="0"
            inactive-value="1"
          >
          </el-switch>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        v-if="
          checkPermi([
            'exam:question:edit',
            'exam:question:remove',
            'exam:question:index',
          ])
        "
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-zoom-in"
            @click="handleGetQuestion(scope.row)"
            v-hasPermi="['exam:question:index']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['exam:question:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            :disabled="ids.length > 0"
            @click="handleDelete(scope.row)"
            v-hasPermi="['exam:question:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getQuestionList"
    />

    <el-dialog title="试题详情" :visible.sync="dialogVisible">
      <div>
        <question :question="question"></question>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listQuestion,
  delQuestion,
  updateQuestionStatus,
  getQuestion,
} from "@/api/exam/question";

import { listSubject } from "@/api/exam/subject";

import question from "@/components/exam/showQuestion/question";

import { checkPermi } from "@/utils/permission"; // 权限判断函数
import { questionTypeEnum } from "@/enum/exam/question";
export default {
  components: { question },
  dicts: ["exam_question_difficult", "exam_question_type"],
  name: "Question",

  data() {
    return {
      // dialog
      dialogVisible: false,
      // 题目详情
      question: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 题目表格数据
      questionList: [],
      //  学科列表
      subjectList: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // 题目名称
        questionTitle: null,
        // 题目类型
        questionType: null,
        subjectId: null,
        // 总分 :fix  el-input-number 默认值问题 设置为undefined
        questionScore: undefined,
        // 难度
        difficult: null,
      },
    };
  },
  created() {
    this.getSubjectList();
    this.getQuestionList();
  },
  methods: {
    // 权限校验
    checkPermi,
    /** 查询学科列表 :分页否？ */
    async getSubjectList() {
      try {
        let { data } = await listSubject(this.queryParams);
        this.subjectList = data.records;
      } catch (error) {
        this.loading = false;
        this.$modal.msgError("获取学科列表失败");
      }
    },
    /** 查询题目列表 */
    async getQuestionList() {
      this.loading = true;
      try {
        let { data } = await listQuestion(this.queryParams);
        this.questionList = data.records;
        this.total = data.total;
        this.loading = false;
      } catch (error) {
        this.loading = false;
        this.$modal.msgError("获取题目列表失败");
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getQuestionList();
    },
    /** 重置搜索中按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.questionId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /*
     修改状态操作
    */
    async handleStatus(row) {
      console.log(row);
      try {
        await updateQuestionStatus(row.questionId, row.status);
        this.$modal.msgSuccess("状态修改成功");
      } catch (error) {
        //   正常点击是不会走这里的，除非自己加参数
        this.getQuestionList();
        console.log("error", error);
      }
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      const { questionId, questionType } = row;
      let path = questionTypeEnum[questionType];
      if (!path || path.length == 0) return;
      this.$router.push({
        path: "/question/add/" + path,
        props: true,
        query: { questionId },
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      let questionIds = this.ids.length == 0 ? [row.questionId] : this.ids;
      this.$modal
        .confirm('是否确认删除题目编号为"' + questionIds + '"的数据项？')
        .then(function () {
          return delQuestion(questionIds);
        })
        .then(() => {
          this.getQuestionList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    /** 获取题目详情 */
    async handleGetQuestion(row) {
      let { data } = await getQuestion(row.questionId);
      this.dialogVisible = true;
      this.question = data;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "exam/question/export",
        {
          ...this.queryParams,
        },
        `question_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
